<template>
  <div class="production-order-container">
    <div class="header">
      <div>
        <el-icon class="back-icon" @click="goBack">
          <ArrowLeft />
        </el-icon>
      </div>
      <div>生产工单</div>
      <div></div>
    </div>

    <!-- 搜索区 -->
    <div class="search-container">
      <el-input v-model="searchValue.orderId" placeholder="请输入订单编号/车号" clearable @clear="handleSearchClear"
        @keyup.enter="handleSearch">
        <template #append>
          <el-button @click="handleSearch">
            <el-icon>
              <Search />
            </el-icon>
          </el-button>
        </template>
      </el-input>
    </div>

    <!-- Tab切换区 -->
    <div class="tab-container">
      <el-tabs v-model="activeTab" @tab-change="handleTabChange">
        <el-tab-pane label="未开工" name="unstarted"></el-tab-pane>
        <el-tab-pane label="已开工" name="started"></el-tab-pane>
        <el-tab-pane label="完工待检" name="waiting"></el-tab-pane>
        <el-tab-pane label="完工" name="completed"></el-tab-pane>
      </el-tabs>
    </div>

    <!-- 数据展示区 -->
    <div class="data-container">
      <div v-if="loading" class="loading-container">
        <el-icon class="is-loading">
          <Loading />
        </el-icon>
        加载中...
      </div>

      <div v-else-if="orderList.length === 0" class="empty-container">
        暂无数据
      </div>

      <div v-else class="order-list">
        <div v-for="(order, index) in orderList" :key="index" class="order-item"
          :class="{ 'selected-order': selectedOrderId === order.orderId }" @click="handleOrderClick(order.orderId)">
          <div class="order-header">
            <span class="order-id">订单编号: {{ order.orderId }}</span>
            <span class="order-status" :class="getStatusClass(order.status)">
              {{ getStatusText(order.status) }}
            </span>
          </div>
          <div class="order-content">
            <div class="order-row">
              <span>车号:</span>
              <span>{{ order.carNumber }}</span>
            </div>
            <div class="order-row">
              <span>工序工单编码:</span>
              <span>{{ order.processOrderCode }}</span>
            </div>
            <div class="order-row">
              <span>工序名称:</span>
              <span>{{ order.processName }}</span>
            </div>
            <div class="order-row">
              <span>工位:</span>
              <span>{{ order.workstation }}</span>
            </div>
            <div class="order-row">
              <span>计划日期:</span>
              <span>{{ order.planDate }}</span>
            </div>
          </div>
          <div class="order-footer">
            <template v-if="order.status === OrderStatus.UNSTARTED">
              <el-button type="primary" size="small" @click.stop="handleStartWork(order)">
                开工
              </el-button>
            </template>

            <template v-else-if="order.status === OrderStatus.STARTED">
              <el-button type="warning" size="small" @click.stop="handleExceptionReport(order)">
                异常上报
              </el-button>
            </template>

            <!-- 其他状态不显示按钮 -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getProductionOrder, startWork, AbnormalReporting } from '@/api/productionOrder';
import { ProductionOrderList, ProductionOrderForm } from '@/api/productionOrder/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const router = useRouter()

enum OrderStatus {
  UNSTARTED = 'unstarted',
  STARTED = 'started',
  WAITING = 'waiting',
  COMPLETED = 'completed'
}

// 搜索相关
const searchValue = ref<ProductionOrderForm>({
  orderId: '',
  status: '',
})
const activeTab = ref('unstarted')
const loading = ref(false)

const selectedOrderId = ref<string | null>(null)
// 订单数据
const orderList = ref<ProductionOrderList[]>([
  {
    orderId: '20240218001',
    carNumber: '999382',
    processOrderCode: '20240218001-10',
    processName: '表面清理',
    workstation: '83320301-喷砂间',
    planDate: '2024-02-14',
    status: OrderStatus.STARTED
  },
  {
    orderId: '20240218002',
    carNumber: '999382',
    processOrderCode: '20240218001-10',
    processName: '表面清理',
    workstation: '83320301-烘干间',
    planDate: '2024-02-14',
    status: OrderStatus.WAITING
  },
  {
    orderId: '20240218003',
    carNumber: '999382',
    processOrderCode: '20240218001-10',
    processName: '喷砂处理',
    workstation: '83320301-表面准备台位',
    planDate: '2024-02-14',
    status: OrderStatus.UNSTARTED
  },
  {
    orderId: '20240218004',
    carNumber: '999382',
    processOrderCode: '20240218001-10',
    processName: '烘干',
    workstation: '83320301-腻子1间',
    planDate: '2024-02-14',
    status: OrderStatus.UNSTARTED
  }
])

// 状态文本映射
const statusTextMap = {
  unstarted: '未开工',
  started: '已开工',
  waiting: '完工待检',
  completed: '完工'
}

// 状态类名映射
const statusClassMap = {
  unstarted: 'status-unstarted',
  started: 'status-started',
  waiting: 'status-waiting',
  completed: 'status-completed'
}

/** 查询SAP订单管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await getProductionOrder(searchValue.value);
  orderList.value = res.rows;
  loading.value = false;
}

// 获取状态文本
const getStatusText = (status: string) => {
  return statusTextMap[status as keyof typeof statusTextMap] || status
}

// 获取状态类名
const getStatusClass = (status: string) => {
  return statusClassMap[status as keyof typeof statusClassMap] || ''
}

// 添加这个方法处理订单点击
const handleOrderClick = (orderId: string) => {
  selectedOrderId.value = selectedOrderId.value === orderId ? null : orderId
}

// 处理搜索
const handleSearch = () => {
  // getList()
}

// 处理搜索清除
const handleSearchClear = () => {
  searchValue.value.orderId = ""
}

// 处理Tab切换
const handleTabChange = (tabName: string) => {
  console.log(tabName, "tabName");
  searchValue.value.status = tabName
  //await getList()
}

// 处理开工操作
const handleStartWork = async (order: any) => {
  const _ids = order?.orderId
  await proxy?.$modal.confirm('是否开工订单编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await startWork(_ids);
  proxy?.$modal.msgSuccess("操作成功");
  //await getList()
}

// 处理异常上报
const handleExceptionReport = async (order: any) => {
  const _ids = order?.orderId
  await proxy?.$modal.confirm('是否异常上报订单编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await AbnormalReporting(_ids);
  proxy?.$modal.msgSuccess("操作成功");
  //await getList()
}


// 返回上一页
const goBack = () => {
  router.back()
}

onMounted(() => {
  // 初始化时可以加载数据
  // getList()
})
</script>

<style lang="scss" scoped>
.production-order-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f5f5f5;
  max-width: 800px;
  margin: 0 auto;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px;
    background-color: #409eff;
    color: white;
    font-size: 18px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

    .back-icon {
      font-size: 24px;
      cursor: pointer;
    }
  }

  .search-container {
    padding: 15px;
    background-color: white;
    border-bottom: 1px solid #eee;
  }

  .tab-container {
    background-color: white;
    padding: 0 15px;

    :deep(.el-tabs__header) {
      margin: 0;
    }

    :deep(.el-tabs__nav-wrap::after) {
      height: 1px;
    }

    :deep(.el-tabs__item) {
      padding: 0 12px;
      font-size: 14px;
    }
  }

  .data-container {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    background-color: #f5f5f5;

    .loading-container,
    .empty-container {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100px;
      color: #999;
    }

    .order-list {
      display: flex;
      flex-direction: column;
      gap: 12px;

      .order-item {
        background-color: white;
        border-radius: 8px;
        padding: 15px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
        position: relative;

        // padding-bottom: 50px; // 为底部按钮留出空间
        &.selected-order {
          background-color: #CAF982 !important;
          transition: background-color 0.3s ease;
        }

        &:hover {
          cursor: pointer;
          box-shadow: 0 2px 6px rgba(0, 0, 0, 0.15);
        }

        .order-header {
          display: flex;
          justify-content: space-between;
          align-items: center;
          margin-bottom: 10px;
          padding-bottom: 8px;
          border-bottom: 1px solid #eee;

          .order-id {
            font-weight: bold;
          }

          .order-status {
            font-size: 12px;
            padding: 3px 8px;

            &.status-unstarted {
              background-color: #f0f0f0;
              color: #666;
            }

            &.status-started {
              background-color: #e6f7ff;
              color: #1890ff;
            }

            &.status-waiting {
              background-color: #fff7e6;
              color: #fa8c16;
            }

            &.status-completed {
              background-color: #f6ffed;
              color: #52c41a;
            }
          }
        }

        .order-content {
          display: flex;
          flex-direction: column;
          gap: 6px;
          font-size: 14px;

          .order-row {
            display: flex;

            span:first-child {
              color: #666;
              min-width: 80px;
            }

            span:last-child {
              color: #333;
            }
          }
        }

        .order-footer {
          position: absolute;
          right: 15px;
          bottom: 15px;

          .exception-btn {
            padding: 7px 15px;
          }
        }
      }
    }
  }
}
</style>